<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class ItemDescription extends MY_Controller {

	function __construct() {
		parent::__construct();
	}
	
	public function index($offer_id = 0)
	{
		if ($this->isSessionActive()) {

			$this->load->model("Itemdescription_model");
			$data['offer'] = $this->Itemdescription_model->get_offer($offer_id);
			$data['similar_items'] = $this->Itemdescription_model->get_similar_items($data['offer']['category_id'], $data['offer']['sub_category_id'], $this->fb_id, $offer_id);
			if ($this->fb_id == $data['offer']['fb_id'])
				$data['requesters'] = $this->Itemdescription_model->get_requesters($offer_id);
			else
				$data['requesters'] = $this->Itemdescription_model->is_requester($offer_id, $this->fb_id);
			$data['comments'] = $this->Itemdescription_model->get_comments($offer_id, $this->fb_id);
			
			$data['title'] = 'Item Description';
			$data['content_view'] = 'itemdescription_view';
			$data['stylesheets'] = array('itemdescription', 'starrating');
			$data['javascripts'] = array('itemdescription', 'slider', 'starrating_itemdescription');

    		$this->load_template_view($data, TRUE);
		}
	}
	
	public function submitcomment()
	{
		$offer_id = $_POST['offer_id'];
		if ($_POST['last_timestamp'])
			$lasttimestamp = $_POST['last_timestamp'];
		else
			$lasttimestamp = '';
		$this->load->model("Itemdescription_model");
		if ($_POST['comment'])
		{
			$comment = $_POST['comment'];
			$new_comments = $this->Itemdescription_model->insert_comment($comment, $this->fb_id, $offer_id, $lasttimestamp);
			echo json_encode($new_comments);
		}
	}
	
	public function deletecomment()
	{
		$id = $_POST['chat_id'];
		$this->load->model("Itemdescription_model");
		$this->Itemdescription_model->delete_comment($id);
	}
	
	/*
	public function getcomment($offer_id)
	{
		$this->load->model("Itemdescription_model");
		if($_POST)
		{
			$last = urldecode($_POST['lastdatetime']);
			$last = date('Y-m-d H:i:s', strtotime($last.'+4 hours'));
			$new_comments = $this->Itemdescription_model->get_new_comments($offer_id, $last);
			echo json_encode($new_comments);
		}
		else
			echo "nothing!";
	}*/
	
	function requestoffer()
	{
		$offer = $_POST['offer_id'];
		$status = $_POST['status'];
		$this->load->model("Itemdescription_model");
		$this->Itemdescription_model->request_offer($offer, $this->fb_id, $status);
		$this->add_status_message('Your request have been noted.');
	}
	
	function giveoffer()
	{
		$offer = $_POST['offer_id'];
		$fb_id = $_POST['fb'];
		$this->load->model("Itemdescription_model");
		$this->load->model("Home_model");
		$this->load->model("Notification_model");
		
		$this->Itemdescription_model->give_offer($offer, $fb_id);
		$details = $this->Home_model->get_offer_details($offer);
		
		$reserved_person = $this->Home_model->get_all_other('request', 'Offered to you', $offer);
		$message = "Congratulations! <a href='".base_url('/profile/')."/".$details[0]['fb_id']."'><span class='get-fb-name'>".$details[0]['fb_id']."</span></a> has reserved the item: <a href='".base_url('/itemdescription/')."/".$details[0]['offer_id']."'>".$details[0]['item_title']."</a> for you! Discuss time/venue to meet up with <a href='".base_url('/profile/')."/".$details[0]['fb_id']."'><span class='get-fb-name'>".$details[0]['fb_id']."</span></a> and click 'Accept Request' once you have obtained the item!";
		$this->Notification_model->create_notification($reserved_person[0]['fb_id'], $message);
		
		$all_others = $this->Home_model->get_all_other('request', 'Offered to someone else', $offer);
		$message = "<a href='".base_url('/itemdescription/')."/".$details[0]['offer_id']."'>".$details[0]['item_title']."</a> has been reserved for someone else. You will be notified if that person decides to reject the offer.";
		if ($all_others)
		{
			foreach($all_others as $person)
			{
				$this->Notification_model->create_notification($person['fb_id'], $message);
			}
		}
	}
	
	function rejectrequester()
	{
		$id = $_POST['offer_id'];
		$this->load->model("Home_model");
		$this->load->model("Notification_model");
		
		$details = $this->Home_model->get_offer_details($id);
		
		$got_others = $this->Home_model->get_all_other('request', 'Offered to someone else', $id);//notify others
		
		if($got_others)
		{
			$this->Home_model->change_status('request', $id, 'Offered to someone else', 'Pending', array('display'=>'1'));
			$message = "<a href='".base_url('/itemdescription/')."/".$details[0]['offer_id']."'>".$details[0]['item_title']."</a> has been made available again. Your request has been noted.";
			foreach($got_others as $person)
			{
				$this->Notification_model->create_notification($person['fb_id'], $message);
			}
		}
		$this->Home_model->change_status('offer', $id, 'Reserved', 'Available');//change status
		$offered_person = $this->Home_model->get_all_other('request', 'Offered to you', $id);//notify reserved
		$this->Home_model->remove_from_database('request', $offered_person[0]['fb_id'], $id);
		$message = "<a href='".base_url('/profile/')."/".$details[0]['fb_id']."'><span class='get-fb-name'>".$details[0]['fb_id']."</span></a> has rejected your reservation for the item: <a href='".base_url('/itemdescription/')."/".$details[0]['offer_id']."'>".$details[0]['item_title']."</a>. If you still want the item, make a request for it again.";
		$this->Notification_model->create_notification($offered_person[0]['fb_id'], $message);
	}
	
	function acceptoffer()
	{
		$offer = $_POST['offer_id'];
		$this->load->model("Itemdescription_model");
		$this->load->model("Home_model");
		$this->load->model("Notification_model");
		$this->Itemdescription_model->accept_offer($offer, $this->fb_id);
		$details = $this->Home_model->get_offer_details($offer);
		$message = "<a href='".base_url('/profile/')."/".$this->fb_id."'><span class='get-fb-name'>".$this->fb_id."</span></a> has received and accepted the item: <a href='".base_url('/itemdescription/')."/".$details[0]['offer_id']."'>".$details[0]['item_title']."</a>. You have closed this offer successfully!";
		$this->Notification_model->create_notification($details[0]['fb_id'], $message);
	}
	
	function canceloffer()
	{
		$status = $_POST['status'];
		$id = $_POST['offer_id'];
		$this->load->model("Home_model");
		$details = $this->Home_model->get_offer_details($id);
		if ($status == 'pending')
		{
			$this->Home_model->remove_from_database('request', $this->fb_id, $id);
		}
		else if ($status == 'accepted')
		{
			$this->Home_model->remove_from_database('request', $this->fb_id, $id);
			$this->load->model("Notification_model");
			
			$message = "<a href='".base_url('/itemdescription/')."/".$details[0]['offer_id']."'>".$details[0]['item_title']."</a> has been made available again. Your request has been noted.";
			$got_others = $this->Home_model->get_all_other('request', 'Offered to someone else', $id);
			if ($got_others)
			{
				$this->Home_model->change_status('request', $id, 'Offered to someone else', 'Pending', array('display'=>'1'));
				foreach($got_others as $person)
					$this->Notification_model->create_notification($person['fb_id'], $message);
			}
			
			$message_for_owner = "<a href='".base_url('/profile/')."/".$this->fb_id."'><span class='get-fb-name'>".$this->fb_id."</span></a> has rejected the item: <a href='".base_url('/itemdescription/')."/".$details[0]['offer_id']."'>".$details[0]['item_title']."</a>. All other interested parties have been notified. You may wish to offer the item to someone else.";
			$this->Home_model->change_status('offer', $id, 'Reserved', 'Available');
			$owner = $this->Home_model->get_owner($id);
			$this->Notification_model->create_notification($owner[0]['fb_id'], $message_for_owner);
		}
	}
}